require "Common/define"
local UIScreenEffect   = UIBase.New(PanelResNames.UIScreenEffect)
local this = UIScreenEffect;
this.camera = nil
this.transform = nil;
---@type GGraph
this.effect1Obj = nil
this.effect2Obj = nil
this.repeatEffectObj = nil
---@type GoWrapper[]
this.goWrapper = nil
this.newTouch = nil
this.touchTime = 0
this.init = false
this.fps = nil;
this.vec3Zero = Vector3.zero
this.screenHeight = nil

local Input = Input
function UIScreenEffect.Init()
    UIBase.Init(UIScreenEffect)
end

--定义控件
---@param obj GComponent
function UIScreenEffect:OnCreate(obj)
    if not this.init then
        this.obj = self.fguiObj
        this.screenHeight = Screen.height
    
        this.repeatEffectObj = self:GetChild("repeat")
        this.effect1Obj = self:GetChild("1")
        this.effect2Obj = self:GetChild("2")
        this.camera = UIParent:GetComponent("Camera");
        this.fps = self:GetChild("fps")
        this.repeatEffectObj.visible = false
        this.effect1Obj.visible = false
        this.effect2Obj.visible = false
        this.fps.visible = false
    
        this.goWrapper = {GoWrapper.New(),GoWrapper.New(),GoWrapper.New()}
        resMgr:LoadEffectPrefab("Fx/UI_boss_dianji",function(prefab)
            for i = 1,3 do
                local p = newObject(prefab)
                --p:AddComponent(TCanvas)
                this.goWrapper[i]:SetWrapTarget(p,false)
            end
            this.repeatEffectObj:SetNativeObject(this.goWrapper[1])
            this.effect1Obj:SetNativeObject(this.goWrapper[2])
            this.effect2Obj:SetNativeObject(this.goWrapper[3])
            for k,v in pairs(this.goWrapper) do
                Util.Lua_SetLocalPositionXYZ(v.wrapTarget.transform,this.vec3Zero.x,this.vec3Zero.y,this.vec3Zero.z);
            end
        end)

        this.init = true
    end
end

function UIScreenEffect.OnEnable()
    this.LocalListenEvent();
    logicMgr.ItemFlyMgr.SetFlyRoot(this.obj)
    UpdateBeat:Add(this.Update, self)
end


function UIScreenEffect:Update()
    if this.init then
        if Input.GetMouseButtonDown(0) then
            local x,y,z = GetMousePosition()--Input.mousePosition
            local localX,localY = FGUIScreenToLocal(FairyGUI.GRoot.inst, x,y) --FGUIScreenToLocal使用GRoot计算时，需要返回值，需要处理黑边问题，非GRoot计算的不需要处理；的FairyGUI.GRoot.inst:GlobalToLocal(tempVec2)
            if this.effect2Obj ~= nil and this.effect2Obj.displayObject then
                this.effect2Obj:SetXY(localX - Config.uiXOffset,localY)
            end
            if this.effect1Obj ~= nil and this.effect1Obj.displayObject then
                this.effect1Obj:SetXY(localX - Config.uiXOffset,localY)
            end
            if this.repeatEffectObj ~= nil and this.repeatEffectObj.displayObject then
                this.repeatEffectObj:SetXY(localX - Config.uiXOffset,localY)
            end
            this.touchTime = Time.time
            this.newTouch = true
        end
        if Input.anyKey then
            if this.newTouch and Time.time - this.touchTime >= 0.25 then
                this.newTouch = false;
                if this.repeatEffectObj ~= nil then
                    this.repeatEffectObj.visible = true
                end
            end
        end
    
        if Input.GetMouseButtonUp(0) then
            if Time.time - this.touchTime < 0.25 then
                --随机播放1或者2
                local num = math.random(10)
                if num >= 7 then
                    if this.effect1Obj then
                        this.effect1Obj.visible = false
                    end
                    if this.effect2Obj then
                        this.effect2Obj.visible = true
                    end
                else
                    if this.effect1Obj then
                        this.effect2Obj.visible = false
                    end
                    if this.effect2Obj then
                        this.effect1Obj.visible = true
                    end
                end
            end
            this.newTouch = false
            if this.repeatEffectObj ~= nil then
                this.repeatEffectObj.visible = false
            end
            FireEvent(Config.EventType.TouchAnyKey)
        end
    end
end

function UIScreenEffect.OnDestroy()
    this.init = false
    this.LocalCloseEvent();
    UpdateBeat:Remove(this.Update, self);
end

function UIScreenEffect.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_FPS, this.Fresh_FPS);
end

function UIScreenEffect.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_FPS, this.Fresh_FPS);
end

function UIScreenEffect.Fresh_FPS(show, value)
    this.fps.visible = show
    local f = string.format("%.2f", value);
    --log(f);
    this.fps.text = "FPS:"..f
end

return UIScreenEffect;